//
//  Perfect_Bridge.cpp
//  HelloWorld
//
//  Created by Kevin Tang on 31/12/14.
//  Copyright (c) 2014年 Kevin Tang. All rights reserved.
//
#include <iostream>
#include <boost/make_shared.hpp>
#include "Perfect_Bridge.h"




namespace perfectBridge {
    using namespace::boost;

    void test_perfect()
    {
        //智能指针，与释构函数一起使用
        shared_ptr<AbStractRoad> pHighRoad( new HighRoad(new Car()) );
        pHighRoad->OpenRoad();
        
        shared_ptr<AbStractRoad> pStreetRoad( new StreetRoad(new Bus()) );
        pStreetRoad->OpenRoad();

        //结合工厂创建模式，使用智能指针,不再需要释放内存
        shared_ptr<StreetRoad> pStreetRoadA(new StreetRoad);
        pStreetRoadA->Set(Bus::Create());
        pStreetRoadA->OpenRoad();
    }
    
    
    AbStractRoad::AbStractRoad()
    {
        std::cout<< " call perfectBridge: AbStractRoad " <<std::endl;
    }
    AbStractRoad::~AbStractRoad()
    {
        std::cout<< " perfectBridge: call ~AbStractRoad " <<std::endl;
    };
    //======================================================================================
    AbStractTransport::AbStractTransport()
    {
        std::cout<< " call perfectBridge: AbStractTransport " <<std::endl;
    }
    AbStractTransport::~AbStractTransport()
    {
        std::cout<< " call perfectBridge: ~AbStractTransport " <<std::endl;
    };
    //======================================================================================
    HighRoad::HighRoad(AbStractTransport* transport):mSport(NULL)
    {
        mSport = transport;
    }
    HighRoad::~HighRoad()
    {
        if (mSport) {
            delete mSport;
            mSport = NULL;
        }
        std::cout<< " call perfectBridge: ~HighRoad " <<std::endl;
    }
    void HighRoad::OpenRoad()
    {
        mSport->Run();
        std::cout<< " on HighRoad " <<std::endl;
    }
    
    //======================================================================================
    StreetRoad::StreetRoad(AbStractTransport* transport):mSport(NULL),mTransport(NULL)
    {
        mSport = transport;
    }
    StreetRoad::StreetRoad():mSport(NULL),mTransport(NULL)
    {
    }
    void StreetRoad::Set(shared_ptr<AbStractTransport> transport)
    {
        mTransport = transport;
    }
    StreetRoad::~StreetRoad()
    {
        if (mSport) {
            delete mSport;
            mSport = NULL;
        }
        std::cout<< " call perfectBridge: ~StreetRoad " <<std::endl;
    }
    void StreetRoad::OpenRoad()
    {
        if (mSport) {
            mSport->Run();
        }
        else if(mTransport){
            mTransport->Run();
        }
        
        std::cout<< " on StreetRoad " <<std::endl;
    }
    //======================================================================================
    Car::Car()
    {
        std::cout<< " call perfectBridge: Car " <<std::endl;
    }
    Car::~Car()
    {
        std::cout<< " call perfectBridge: ~Car " <<std::endl;
    }
    void Car::Run()
    {
        std::cout<< " Car run" ;
    }
    //======================================================================================
    shared_ptr<AbStractTransport> Bus::Create()
    {//工厂创建模式，返回一个智能指针。对象的释放，不需要delete
         return shared_ptr<AbStractTransport> (new Bus);
    }
    
    Bus::Bus()
    {
        std::cout<< " call perfectBridge: Bus " <<std::endl;
    }
    Bus::~Bus()
    {
        std::cout<< " call perfectBridge: ~Bus " <<std::endl;
    }
    void Bus::Run()
    {
        std::cout<< " Bus run" ;
    }
    
}



















